package gui.RepairType;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

import gui.PrinterDialog;
import model.*;

public class RepairTypeListFrame extends JFrame {

	private JButton create, update, delete, printer, printerAll;

	private Controller controller = new Controller();

	protected JTable m_table;
	protected RTJTable_StockTableData m_data;

	public RepairTypeListFrame() {
		setTitle("RepairTypes");
		setBounds(200, 200, 600, 300);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		getContentPane().setLayout(new BorderLayout());

		getTable();
		getButtons();
	}

	private void getTable() {
		UIManager.put("Table.focusCellHighlightBorder", new LineBorder(
				Color.black, 0));

		m_data = new RTJTable_StockTableData();
		m_table = new JTable();
		m_table.setAutoCreateColumnsFromModel(false);
		m_table.setModel(m_data);
		for (int k = 0; k < m_data.getColumnCount(); k++) {
			DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
			renderer
					.setHorizontalAlignment(RTJTable_StockTableData.m_columns[k].m_alignment);
			TableColumn column = new TableColumn(k,
					RTJTable_StockTableData.m_columns[k].m_width, renderer,
					null);
			m_table.addColumn(column);
		}
		JTableHeader header = m_table.getTableHeader();
		header.setUpdateTableInRealTime(false);
		JScrollPane ps = new JScrollPane();
		ps.getViewport().setBackground(m_table.getBackground());
		ps.getViewport().add(m_table);
		getContentPane().add(ps, BorderLayout.CENTER);
	}

	private void getButtons() {
		JPanel pButtons = new JPanel();
		getContentPane().add(pButtons, BorderLayout.SOUTH);

		create = new JButton("Create");
		create.addActionListener(controller);
		pButtons.add(create);

		update = new JButton("Update");
		update.addActionListener(controller);
		pButtons.add(update);

		delete = new JButton("Delete");
		delete.addActionListener(controller);
		pButtons.add(delete);

		printer = new JButton("Spare Part History");
		printer.addActionListener(controller);
		pButtons.add(printer);

		printerAll = new JButton("All Spare Part History");
		printerAll.addActionListener(controller);
		pButtons.add(printerAll);
	}

	private List<RepairType> m_list;

	public void fillListRepairType() {
		m_list = new ArrayList<RepairType>();

		GregorianCalendar gc = new GregorianCalendar(2008,
				GregorianCalendar.JANUARY, 9, 14, 12, 42);
		// -----------------------------------
		MachineType mt = new MachineType(2345678, "SP3000", "");
		// -----------------------------------
		Machine m = new Machine(1234567, "Machine01", "", mt);
		// -----------------------------------
		Reparation r1 = new Reparation(3132123, gc, "", m);
		// -----------------------------------
		SparePart sp = new SparePart(2203459, 331, 311, 321, "SpR1", 3, 32, 7,
				"for GS20");
		SparePart sp1 = new SparePart(1111111, 111, 111, 000, "ring26R1", 123,
				120, 25, "for GS20");
		SparePart sp2 = new SparePart(2202334, 331, 311, 321, "Sp2R1", 30, 32,
				7, "for GS32");

		sp.createSparePartHistory(r1, new SparePartUse(3, sp));
		sp1.createSparePartHistory(null, new SparePartUse(2, sp1));
		sp2.createSparePartHistory(r1, new SparePartUse(7, sp2));
		// -----------------------------------
		r1.endReparation(new GregorianCalendar(2008, GregorianCalendar.MAY, 10,
				18, 14, 32));
		// -----------------------------------
		GregorianCalendar gc2 = new GregorianCalendar(2008,
				GregorianCalendar.MAY, 12, 14, 12, 42);
		Reparation r2 = new Reparation(3132132, gc2, "", m);
		// -----------------------------------
		SparePart sp21 = new SparePart(2203459, 331, 311, 321, "SpR2", 3, 32,
				7, "for GS20");
		SparePart sp122 = new SparePart(1111111, 111, 111, 000, "ring26R2",
				123, 120, 25, "for GS20");
		SparePart sp223 = new SparePart(2202334, 331, 311, 321, "Sp2R2", 30,
				32, 7, "for GS32");

		sp21.createSparePartHistory(r2, new SparePartUse(12, sp21));
		sp122.createSparePartHistory(r2, new SparePartUse(4, sp122));
		sp223.createSparePartHistory(r2, new SparePartUse(2, sp223));
		m_list.clear();

		// ------------------------------------------------------
		RepairType rt1 = new RepairType(1010101, "AAA", "Power upgrade");
		RepairType rt2 = new RepairType(1010102, "AAB", "Pressure turbulence");

		rt1.addSparePartUse(new SparePartUse(6, sp));
		rt1.addSparePartUse(new SparePartUse(1, sp1));
		rt2.addSparePartUse(new SparePartUse(5, sp21));
		rt2.addSparePartUse(new SparePartUse(3, sp223));

		m_list.add(rt1);
		m_list.add(rt2);

		m_data.setDefaultData(m_list);
	}

	private class Controller implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == create) {
				RepairTypeCUDialog repC = new RepairTypeCUDialog(
						RepairTypeListFrame.this, "Create a RepareType");
				// Conference conference =
				// (Conference)listofConferences.getSelectedValue();
				// repC.setConference(conference);
				repC.setVisible(true);
			}

			if (e.getSource() == printer) {
				if (m_table.getSelectedRow() == -1) {
					return;
				}
				int index = m_table.getSelectedRow();
				RepairType repairType = (RepairType) m_list.get(index);

				PrinterDialog repSPH = new PrinterDialog(
						RepairTypeListFrame.this, "-<<RepairType("
								+ repairType.getName() + ") Status>>-", "HI");
				repSPH.setVisible(true);
			}

			if (e.getSource() == printerAll) {
				String allText = "";
				// TODO
				for (@SuppressWarnings("unused")
				RepairType o : m_list) {
					// RepairType r = (RepairType) o;
					// allText += r.getSparePartHistoryPrint(m_data
					// .getSimpleDateFormat())
					// + "\n";
				}

				PrinterDialog repSPH = new PrinterDialog(
						RepairTypeListFrame.this, "All SPH from RepairTypes",
						allText);
				repSPH.setVisible(true);
			}
		}
	}
}